package com.lw.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/4/25 11:37
 */
public class WebsocketInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline()
                //因为websocket是基于http升级出来的，所以先要添加http支持
                .addLast("httpcodec",new HttpServerCodec())
                //我们的websocket的数据是一块一块的，需要支持这种数据格式
                .addLast(new ChunkedWriteHandler())
                //数据的最大值
                .addLast(new HttpObjectAggregator(8192))
                //websocket的连接地址，注意连接时候后面不要写/  要访问 locahost:12345/abc  而不能是 locahost:12345/abc/
                .addLast(new WebSocketServerProtocolHandler("/abc"))
                .addLast(new MyWebsocketHandler());

    }
}
